
***************************************************
*Table A.2
***************************************************
***************************************************
*Table A.2, top
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

g e=1
g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************

eststo clear
local model=0
foreach y of varlist wage sa {
forvalues bw=1/3 {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

forvalues s=1/1{
preserve
local age_min=25-`bw'
local age_max=25+`bw'
keep if age_q>=`age_min' & age_q<`age_max'

sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

local bw_num=`bw'
if `y'==wage local title="Employment"
if `y'==sa   local title="Social Assistance"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05
if `s'==1 local spec="d age_q0 age_q0_2 dXage_q0 dXage_q0_2"
if `s'==2 local spec="d age_q0"
if `s'==1 local spec_name="2nd order polynomial spline"
if `s'==2 local spec_name="Linear"
if `bw'==1 local bw_name="+/- `bw' year"
if `bw'!=1 local bw_name="+/- `bw' years"

g w2=e                             
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=w2] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/tableA_2_1,  addlabel(samplesize,`obs',N_clust,`N_clust',outcome,`y',bw,`bw_name',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

restore
}
}
}

***************************************************
*Table A.2, bottom
***************************************************
use "$workdata/baseline", clear /* see $data_do/baseline.do */

g e=1
g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************

eststo clear
local model=0
foreach y of varlist wage sa {
forvalues bw=1/4 {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

forvalues s=1/1{
preserve
local age_min=25-`bw'
local age_max=25+`bw'
keep if age_q>=21 & age_q<29

sum `y' if age_month_q==24.11
local initial=round(r(mean), 0.01)
di `initial'

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

local bw_num=`bw'
if `y'==wage local title="Employment"
if `y'==sa   local title="Social Assistance"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05
if `s'==1 local spec="d age_q0                   dXage_q0"
if `s'==2 local spec="d age_q0"
if `s'==1 local spec_name="Linear spline"
if `s'==2 local spec_name="Linear"
if `bw'==1 local bw_name="+/- `bw' year"
if `bw'!=1 local bw_name="+/- `bw' years"

g w=max(0,`bw'-abs(age_q0))
g w2=w*e
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=w2] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'

regsave d using $tables/tableA_2_2,  addlabel(samplesize,`obs',N_clust,`N_clust',outcome,`y',bw,`bw_name',spec,`spec_name',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

restore
}
}
}

***************************************************
*end: Table A.2
***************************************************
